package com.situ.struts.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

public class DataAction1 extends ActionSupport {

    @Override
    public String execute() throws Exception {
        System.out.println("DataAction1.execute");
        // 1、直接获取原生的Servlet API，使用的是ServletActionContext提供的静态方法
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpSession session = request.getSession();
        ServletContext application = ServletActionContext.getServletContext();
        request.setAttribute("request_data1", "request_data1111");
        session.setAttribute("session_data1", "session_data1111");
        application.setAttribute("application_data1", "application_data1111");

        // 2、通过ActionContext获取不同的代表requst、session、application的map对象
        ActionContext actionContext = ActionContext.getContext();
        // Struts对HttpServletRequst对象进行封装，封装为一个map，往这个map里面put值其实就是往request里面放值
        Map<String, Object> requstMap = actionContext.getContextMap();
        Map<String, Object> sessionMap = actionContext.getSession();
        Map<String, Object> applicationMap = actionContext.getApplication();
        requstMap.put("request_data2", "request_data2222");
        sessionMap.put("session_data2", "session_data2222");
        applicationMap.put("application_data2", "application_data2222");
        return "data";
    }
}
